package com.tuanbiao.presenter;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.abiao.lib_utils.ActivityUtils;
import com.hzjq.data.entity.PlanEntity;
import com.hzjq.lib.BlastKool;
import com.hzjq.lib.ble.BleBus;
import com.hzjq.lib.checker.CheckError;
import com.hzjq.lib.config.param.BlastLoader;
import com.hzjq.lib.wiget.RepeatDialog;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.SimpleCallback;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.PlanController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.models.PlanModel;

import java.util.List;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/11 14:46
 */
public class PlanManagePresenter extends BasePresenter<PlanModel, PlanController.View> implements PlanController.Presenter {
    public PlanManagePresenter(PlanModel model, PlanController.View rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    @Override
    public void refresh() {
        mRootView.showStatistic(0, 0, 0);
        refreshPlanList();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private void showRepeatCapDialog() {
        BlastKool.Companion.loader()
                .setOnPlanCheckRepeatResultCallback(repeatResult -> {
                            RepeatDialog dialog = RepeatDialog.Companion.show(repeatResult);
                            dialog.popupInfo.xPopupCallback = new SimpleCallback() {
                                @Override
                                public void onDismiss(BasePopupView popupView) {
                                    refresh();
                                }
                            };
                        }
                );
    }

    @Override
    public void getPlanList() {
        mModel.getPlanList(new OnBaseResultCallback<List<PlanEntity>>() {

            @Override
            public void onSuccess(List<PlanEntity> planList) {
                if(mRootView == null) return;
                mRootView.setPlantItems(planList);
            }

            @Override
            public void onFailed(String msg) {
                mRootView.refreshFailed(msg);
                mRootView.showMessage(msg);
            }
        });
    }

    @Override
    public void refreshPlanList() {
        mRootView.showLoading();
        mModel.refreshPlan(new OnBaseResultCallback<List<PlanEntity>>() {

            @Override
            public void onSuccess(List<PlanEntity> planList) {
                mRootView.setPlantItems(planList);
            }

            @Override
            public void onFailed(String msg) {
                mRootView.refreshFailed(msg);
                mRootView.showMessage(msg);
            }

            @Override
            public void onFinish() {
                mRootView.hideLoading();
            }
        });
    }

    @Override
    public void insert(PlanEntity plan) {
        mModel.insertPlan(plan, unused -> refresh());
    }

    @Override
    public void update(PlanEntity plan) {
        mModel.updatePlan(plan, unused -> {
            mRootView.onPlanChange(plan);
            mModel.refreshAuth();
        });
    }

    @Override
    public void select(boolean select) {
        mModel.selectPlan(select, unused -> refresh());
    }

    @Override
    public void delete(PlanEntity plan) {
        mModel.deletePlan(plan, unused -> mRootView.onPlanDelete(plan));
    }

    @Override
    public void delete(List<PlanEntity> plans) {
        mModel.deletePlan(plans, unused -> refresh());
    }

    @Override
    public void auth(PlanEntity plan) {
        if (plan.getCapCount() <= 0) {
            mRootView.showMessage("没有雷管数据");
            return;
        }
//        if (plan.getUnAuthCount() <= 0) {
//            mRootView.showMessage("已全部授权");
//            return;
//        }
        if (mModel.getSettingParams().isStandAloneModel()) {
            mRootView.showMessage("单机模式下无需授权");
        } else {
            mRootView.startAuthOnline(plan);
        }

    }

    @Override
    public void merge(String name, List<PlanEntity> plans) {
        Timber.i(TAG + "#merge#name:" + name + ",plans:" + plans);
        mModel.mergePlan(name, plans, new OnBaseResultCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean success) {
                if (success) {
                    refresh();
                } else {
                    mRootView.showMessage("合并失败请重试");
                }
            }

            @Override
            public void onFailed(String msg) {
                mRootView.showMessage(msg);
            }

        });
    }

    @Override
    public void startDetonatorRegister(PlanEntity plan) {
        if (plan.getCapCount() >= getMaxCapCount()) {
            mRootView.showMessage("最大设置" + getMaxCapCount() + "发");
        } else {
            mRootView.startDetonatorRegister(plan);
        }
    }


    @Override
    public void startBlast() {
        mRootView.showCheckDialog();
        mModel.checkNetted(error -> {
            mRootView.hideCheckDialog();
            if (error.getErrorCode() == CheckError.CHECK_SUCCESS) {
                mRootView.startBlast();
            } else {
                mRootView.showMessage(error.getErrorMsg());
            }
        });
    }

    @Override
    public void share(@NonNull PlanEntity plan) {
        if (plan.getCapCount()<=0) {
            mRootView.showMessage("方案无雷管，无需分享");
            return;
        }
        mModel.sharePlan(String.valueOf(plan.getId()));
    }

    public int getMaxCapCount() {
        return BlastLoader.Companion.getMaxSupportPlanCapSize();
    }
}
